<?php 

require_once ('includes/config.inc.php'); 
require_once (MYSQL);

// Start output buffering:
ob_start();

// Initialize a session:
@session_start();


// Check for a $page_title value:
if (!isset($page_title)) {
	$page_title = 'Review your campaign';
}

// If no first_name session variable exists, redirect the user:
if (!isset($_SESSION['first_name'])) {
	
	$url = BASE_URL . 'index.php'; // Define the URL.
	ob_end_clean(); // Delete the buffer.
	header("Location: $url");
	exit(); // Quit the script.
	
}

include ('includes/header.html');

		if ( !isset( $_SESSION['adtype'], $_SESSION['age'], $_SESSION['gender'], $_SESSION['ethnicity'], 
			$_SESSION['handset'], $_SESSION['interest'], $_SESSION['location'], $_SESSION['income'], $_SESSION['influenceScore'],
			$_SESSION['template'], $_SESSION['campaign'], $_SESSION['company'], $_SESSION['subject'], $_SESSION['message'], $_SESSION['user_id']) )
			{
				$url = BASE_URL . 'index.php'; // Define the URL.
				ob_end_clean(); // Delete the buffer.
				header("Location: $url");
				exit(); // Quit the script.
			}
			
		// Process the form	
		$user_id = $_SESSION['user_id'];
		$campaign = $dbc->real_escape_string($_SESSION['campaign']);		
		$company = $dbc->real_escape_string($_SESSION['company'] );
		$template = $_SESSION['template'];
		$subject = $dbc->real_escape_string($_SESSION['subject']);
		$message = $dbc->real_escape_string($_SESSION['message']);
		$influenceScore = $_SESSION['influenceScore'];
		
		
		$adtype = 0;
		foreach ( $_SESSION['adtype'] as $type )
		{
			$adtype = $adtype | $type;
		}
		
		$age = 0;
		foreach ( $_SESSION['age'] as $type )
		{
			$age = $age | $type;
		}
			
		$gender = 0;
		foreach ( $_SESSION['gender'] as $type )
		{
			$gender = $gender | $type;
		}
		
		$ethnicity = 0;
		foreach ( $_SESSION['ethnicity'] as $type )
		{
			$ethnicity = $ethnicity | $type;
		}	
		
		$handset = 0;
		foreach ( $_SESSION['handset'] as $type )
		{
			$handset = $handset | $type;
		}	

		$location = 0;
		foreach ( $_SESSION['location'] as $type )
		{
			$location = $location | $type;
		}	

		$interest = 0;
		foreach ( $_SESSION['interest'] as $type )
		{
			$interest = $interest | $type;
		}	

		$income = 0;
		foreach ( $_SESSION['income'] as $type )
		{
			$income = $income | $type;
		}				
	
	
		
		
		$query  = "INSERT INTO `campaigns` VALUES ( NULL, '$user_id', '$campaign', '$company', '$adtype', '$age', '$gender', '$ethnicity','$handset',
					'$interest', '$location', '$income', '$influenceScore', '$template', '$subject', '$message' )";
		//echo $query;
		
		@$query_run = mysqli_query($dbc, $query);
		
		if ( $query_run )
		{
			echo '<h2>Successfully Added Campaign to Database</h2>';
			//unset($_SESSION['campaign']);
		}
		else
		{
			echo '<p class="error">Did not add Campaign sucessfully.</p>';
		}
		
		$query	= "SELECT `id`, `phone` FROM `subscribers` WHERE `age`&$age > 0 AND `gender`&$gender > 0 AND
				`ethnicity`&$ethnicity > 0 AND `handset`&$handset > 0 AND `interest`&$interest > 0 AND `location`&$location > 0 
				AND `income`&$income > 0 AND `influenceScore`>$influenceScore";
		
		echo $query;
		
		@$query_run = mysqli_query($dbc, $query);
		$temp = 0;
		if ( $query_run )
		{
			echo "<h3>There are ".$query_run->num_rows." subscribers with your selected preferences:</h3>";
			if ($query_run && $query_run->num_rows > 0)
			{
				echo '<table border=1>';
				echo '<tr><td><b>Subscriber ID</b></td><td><b>Phone Number</b></td></tr>';
				while ($abc = $query_run->fetch_assoc()) {
					printf('<tr><td>%d</td><td>%s</td></tr>', 
					$abc['id'], $abc['phone']);
				}
				echo '</table>';
		
			}		
		}
		
		
			
		// Display what campaigns are in the database
		
		$query = "SELECT  `campaign`, `company`, `adtype`, `subject`, `message` FROM `campaigns` WHERE `user_id`='$user_id'";
		$query_run = mysqli_query($dbc, $query);
		
		if ($query_run && $query_run->num_rows > 0)
		{
			echo '<h3>Here is a list of your current campaigns:</h3>';
			echo '<table border=1>';
			echo '<tr><td><b>Campaign Name</b></td><td><b>Company Name</b></td><td><b>Advertisement Type</b></td><td><b>Subject</b></td><td><b>Message</br></td></tr>';
			while ($abc = $query_run->fetch_assoc()) {
				printf('<tr><td>%s</td><td>%s</td><td>%d</td><td>%s</td><td>%s</td></tr>', 
				$abc['campaign'], $abc['company'], $abc['adtype'], $abc['subject'], $abc['message']);
			}
			echo '</table>';
		}		
?>
<br>
<form action="index.php">
<input type="submit" value="Add Another Campaign">
</form>

			
	
		
